package it.leehook.mp3.adapter;

import it.leehook.mp3.R;
import it.leehook.mp3.utils.SongUtils;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagOptionSingleton;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

/**
 * @author l.angelini
 * 
 */
public class TagAdapter extends BaseAdapter implements Filterable {

	private LayoutInflater mInflater;
	private List<File> list;

	public TagAdapter(Context context, List<File> myList) {
		mInflater = LayoutInflater.from(context);
		this.list = myList;
	}

	public Filter getFilter() {
		return null;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		TagView res;
		convertView = mInflater.inflate(R.layout.tag, parent, false);
		res = new TagView();
		final File file = (File) getItem(position);
		res.nomeFile = (TextView) convertView.findViewById(R.id.nomeFile);
		res.nomeFile.setText(WordUtils.capitalizeFully(file.getName()));
		res.nomeFile.setTextColor(Color.WHITE);
		convertView.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				final Dialog dialog = new Dialog(v.getContext());
				dialog.setContentView(R.layout.edit_tag);
				dialog.setTitle(file.getName());
				dialog.getWindow().setTitleColor(Color.YELLOW);
				dialog.setCancelable(true);
				/*
				 * Mostro le informazioni di ogni file a cui vanno aggiunti i
				 * tag
				 */
				final EditText artist = (EditText) dialog
						.findViewById(R.id.artistaSong);
				final EditText album = (EditText) dialog
						.findViewById(R.id.albumSong);
				final EditText title = (EditText) dialog
						.findViewById(R.id.titoloSong);

				if (StringUtils.endsWithIgnoreCase(file.getName(), "mp3")) {
					try {
						String[] tags = SongUtils.getMp3Tags(file);
						artist.setText(tags[0]);
						album.setText(tags[1]);
						title.setText(tags[2]);
					} catch (Exception e) {
						Log.e("TagAdapter", e.getMessage());
					}
				}
				Button saveBtn = (Button) dialog.findViewById(R.id.saveTagsBtn);
				saveBtn.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						AudioFile audio = SongUtils.getAudioFile(file);
						Tag tag = audio.getTag();
						try {
							tag.addField(FieldKey.ARTIST, WordUtils
									.capitalizeFully(artist.getText()
											.toString()));
							tag.addField(FieldKey.TITLE,
									WordUtils.capitalizeFully(title.getText()
											.toString()));
							tag.addField(FieldKey.GENRE, "Rock");
							tag.addField(FieldKey.ALBUM, album.getText()
									.toString());
							TagOptionSingleton.getInstance().setAndroid(true);
							audio.setTag(tag);
							audio.commit();
							dialog.dismiss();
						} catch (Exception e) {
							Log.e("TagAdapter", e.getMessage());
						}
					}
				});
				dialog.show();
			}
		});
		convertView.setTag(res);
		return convertView;
	}

	static class TagView {
		TextView nomeFile;
	}
}
